# -*-coding:utf-8-*-

import json
import time
import re
import random

from pyrestful import mediatypes
from pyrestful.rest import get, post, put, delete
from bson.objectid import ObjectId

from handlers.base import BaseHandler
from utils.auth import authenticated
from utils.sign import signed
import settings
from alidayu.topnado import Topnado
from tornado.gen import coroutine, Return


class AppsHandler(BaseHandler):
    def __init__(self, application, request, **kwargs):
        super(AppsHandler, self).__init__(application, request, **kwargs)
        self.delegate_name = 'apps'

    def check_permissions(self, permissions):
        return True

    @post(_path='/platform/api/apps', _types=[dict],
          _consumes=mediatypes.APPLICATION_JSON)
    @authenticated
    @coroutine
    def create_app(self, request):
        user_id = self.user.id_str
        developer_id = request.get('developer_id', None)
        if developer_id is None:
            request.update(developer_id=user_id)
        else:
            result = yield self.users_dao.get_user_by_id(None, developer_id)
            if result is None:
                raise Exception('user(%s) not exists' % developer_id)
        result = yield self.apps_dao.create_app(user_id, request)
        self.respond_success(result)

    @get(_path='/platform/api/apps')
    @authenticated
    @coroutine
    def get_app_list(self):
        kwargs = self.query_arguments()
        filter_cond = {'user_id': self.user.id_str}
        result = yield self.apps_dao.get_paging_result(filter_cond, None, None, None, **kwargs)
        self.respond_success(result)

    @get(_path='/platform/api/apps/{aspect}', _types=[str])
    @authenticated
    @coroutine
    def get_app_aspect(self, aspect):
        if 'count' == aspect:
            filter_cond = {'user_id': self.user.id_str}
            result = yield self.apps_dao.count(filter_cond)
        else:
            aid = aspect
            result = yield self.apps_dao.get_app(None, app_id=aid, user_id=self.user.id_str)
            result.data.pop('_id')
            result = result.data
        self.respond_success(result)

    @put(_path='/platform/api/apps/{aid}', _types=[str, dict],
         _consumes=mediatypes.APPLICATION_JSON)
    @authenticated
    @coroutine
    def update_app(self, aid, request):
        developer_id = request.get('developer_id', None)
        if developer_id is not None:
            result = yield self.users_dao.get_user_by_id(None, developer_id)
            if result is None:
                raise Exception('user(%s) not exists' % developer_id)
        result = yield self.apps_dao.update_app(self.user.id_str, aid, request)
        self.respond_success(result)

    @put(_path='/platform/api/apps/{aid}/{aspect}', _types=[str, str, dict],
         _consumes=mediatypes.APPLICATION_JSON)
    @authenticated
    @coroutine
    def update_app_aspect(self, aid, aspect, request):
        result = yield self.apps_dao.update_app_aspect(self.user.id_str, aid, aspect, request)
        self.respond_success(result)

    @delete(_path='/platform/api/apps/{aid}/{aspect}', _types=[str, str])
    @authenticated
    @coroutine
    def delete_app_aspect(self, aid, aspect):
        result = yield self.apps_dao.delete_app_aspect(self.user.id_str, aid, aspect)
        self.respond_success(result)

    @delete(_path='/platform/api/apps/{aid}', _types=[str])
    @authenticated
    @coroutine
    def delete_app(self, aid):
        result = yield self.apps_dao.delete_app(self.user.id_str, aid)
        self.respond_success(result)
